import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
  ApiBody,
} from '@nestjs/swagger';
import type { CreateActivityDto, UpdateActivityDto, ActivityQueryDto } from './activity.dto';
import type { PaginationResult } from '../utils/pagination.utils';
import { PaginationUtils } from '../utils/pagination.utils';
import { ActivityService } from './activity.service';
import type { Activity } from './activity.service';

@ApiTags('游玩项目')
@Controller('activities')
export class ActivityController {
  constructor(private readonly activityService: ActivityService) {}

  @ApiOperation({ summary: '获取游玩项目列表' })
  @ApiResponse({ status: 200, description: '成功获取游玩项目列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码' })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量' })
  @ApiQuery({ name: 'keyword', required: false, description: '关键词搜索' })
  @ApiQuery({ name: 'location', required: false, description: '地点筛选' })
  @ApiQuery({ name: 'status', required: false, description: '状态筛选' })
  @Get()
  findAll(@Query() query: ActivityQueryDto): PaginationResult<Activity> {
    const { page = 1, limit = 10, keyword, location, status } = query;
    let activities = this.activityService.findAll();
    
    if (keyword) {
      activities = activities.filter(
        a => a.name.includes(keyword) || a.description.includes(keyword)
      );
    }
    
    if (location) {
      activities = activities.filter(a => a.location.includes(location));
    }
    
    if (status) {
      activities = activities.filter(a => a.status === status);
    }
    
    return PaginationUtils.paginate(activities, page, limit);
  }

  @ApiOperation({ summary: '根据ID获取游玩项目信息' })
  @ApiResponse({ status: 200, description: '成功获取游玩项目信息' })
  @ApiResponse({ status: 404, description: '游玩项目信息不存在' })
  @Get(':id')
  findOne(@Param('id') id: string): Activity {
    return this.activityService.findOneById(id);
  }

  @ApiOperation({ summary: '创建游玩项目' })
  @ApiResponse({ status: 201, description: '游玩项目创建成功' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '游玩项目数据' })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Body() createActivityDto: CreateActivityDto): Activity {
    return this.activityService.create(createActivityDto);
  }

  @ApiOperation({ summary: '更新游玩项目' })
  @ApiResponse({ status: 200, description: '游玩项目更新成功' })
  @ApiResponse({ status: 404, description: '游玩项目信息不存在' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '游玩项目更新数据' })
  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() updateActivityDto: UpdateActivityDto,
  ): Activity {
    return this.activityService.update(id, updateActivityDto);
  }

  @ApiOperation({ summary: '删除游玩项目' })
  @ApiResponse({ status: 204, description: '游玩项目删除成功' })
  @ApiResponse({ status: 404, description: '游玩项目信息不存在' })
  @ApiBearerAuth()
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param('id') id: string): void {
    this.activityService.remove(id);
  }

  @ApiOperation({ summary: '预订游玩项目' })
  @ApiResponse({ status: 200, description: '成功预订游玩项目' })
  @ApiResponse({ status: 404, description: '游玩项目信息不存在' })
  @ApiResponse({ status: 400, description: '活动已满员' })
  @ApiBearerAuth()
  @ApiBody({ schema: { type: 'object', properties: { bookingId: { type: 'string' } } } })
  @Post(':id/book')
  bookActivity(
    @Param('id') id: string,
    @Body('bookingId') bookingId: string,
  ): Activity {
    return this.activityService.bookActivity(id, bookingId);
  }
}